#pragma once
#include "stdio.h"
#include "string"
#include "map"
#include "IVFileManager.h"

struct FILEINFO
{
	FILEINFO() : pos(0),nPressedSize(0),nRealSize(0),pBuffer(NULL){}
	std::string filename;
	__int64 pos;
	__int64 nPressedSize;
	__int64 nRealSize;
	char *pBuffer;
};

class CVirtualFileManager :public IVirtualFileManager
{
public:
	CVirtualFileManager(void);
	~CVirtualFileManager(void);

	bool OpenPackage(const char *lpzFileName, OpenMode mode =  FO_READ);
	void Close();
	bool PackDirectory(const char *lpzDir, const char *lpzVirtualPath = NULL, IPackCallback *pCallback = NULL);
	bool ExtractDirectory(const char *src, const char *dest, IPackCallback *pCallback = NULL);

	bool PackFile(const char *lpzFullPath, const char *lpzVirtualPath);
	bool ExtractFile(const char *src, const char *dest);
	bool ExtractAllFiles(const char *dest, IPackCallback *pCallback = NULL);

	IVirtualFile *OpenFile(const char *lpzFileName);

private:
	bool WriteFileIndex();
	void ReadFileIndex();
	void MakeTargetPath(std::string targetPath);

private:
	IOSFile *m_hFile;
	FILEHEADER m_stHeader;
	__int64 m_llCurrentPos;
	bool bModified;
	std::map<std::string, FILEINFO> m_mapFiles;
};
